package Base.线程的让步;

/**
 * @author 陈鹏
 * yield()方法为核心
 */
public class Example09 {
    public static void main(String[] args) {
        Thread thread1=new YieldThread("thread1");
        Thread thread2=new YieldThread("thread2");
        thread1.start();
        thread2.start();
    }
}

class YieldThread extends Thread{
    public YieldThread(String name){
        super(name);
    }
    @Override
    public void run(){
        for (int i=0;i<5;i++){
            System.out.println(Thread.currentThread().getName()+"----"+i);
            if (i==2){
//                yield方法会让优先级高于自己的先运行
                System.out.print("线程让步:");
                Thread.yield();
            }
        }
    }
}
